package com.example.xxtautosignjava.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.xxtautosignjava.common.R;
import com.example.xxtautosignjava.entity.Course;
import com.example.xxtautosignjava.entity.CourseAndActiveId;
import com.example.xxtautosignjava.entity.CourseStatus;
import com.example.xxtautosignjava.entity.vo.CourseVo;
import com.example.xxtautosignjava.entity.vo.GetSignInVo;
import com.example.xxtautosignjava.entity.vo.LoginInVo;
import com.example.xxtautosignjava.entity.vo.SignInfoVo;
import com.example.xxtautosignjava.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/xxt/login")
@Scope("prototype")
public class LoginController {
    @Autowired
     private LoginService loginService;

    @GetMapping("getlogin/{phone}/{pwd}")
    public R getLogin(String phone, String pwd) throws IOException, URISyntaxException {
        String login = loginService.getLogin(phone, pwd);
//        String info = login.replaceAll("\\","");

        return R.ok().data("userinfo",login);
    }
    @ApiIgnore
    @PostMapping("login")
    public R getLogin(@RequestBody LoginInVo login) throws IOException, URISyntaxException {
        String getLogin =loginService.getLogin(login.getPhone(),login.getPassword());
        JSONObject jsonObject=(JSONObject) JSON.parse(getLogin);
//        return R.ok().data("userinfo",jsonObject).data("result",jsonObject.get("result"));
        boolean result=(Boolean)jsonObject.get("result");
        System.out.println(result);
        if (result){
            return R.ok().data("userinfo",jsonObject);
        }

        return R.error().data("result",jsonObject.get("errorMsg"));
    }
    @GetMapping("getCourse/{phone}/{pwd}")
    public R getCourse(@PathVariable String phone,@PathVariable String pwd) throws IOException, URISyntaxException {
        ArrayList<Course> couse = loginService.getCouse(phone, pwd);
        return R.ok().data("course",couse);

    }

    @GetMapping("getActiveID/{phone}/{pwd}")
    public R getActiveID(@PathVariable String phone,@PathVariable String pwd ) throws IOException, URISyntaxException {
        List<CourseAndActiveId> activeId = loginService.getActiveId(phone,pwd );
        return R.ok().data("activeId",activeId);

    }


    @PostMapping("getCourseStatus")
    public R getCourseStatus( String phone,String pwd,@RequestBody CourseVo course) throws IOException, URISyntaxException {
        List<CourseStatus> courseStatus = loginService.getCourseStatus(phone, pwd, course);
        return R.ok().data("CourseStatusList",courseStatus);
    }
    @PostMapping("getSignInfo")
    public R getSignInfo(String phone, String pwd  ,@RequestBody GetSignInVo getSignInVo) throws IOException, URISyntaxException {
        SignInfoVo signInfo = loginService.getSignInfo(phone, pwd, getSignInVo);
        return R.ok().data("signInfo",signInfo);
    }


}
